// index.js
Page({
  data:{
    tab: 0,
    item: 0,
    healthlist:[],
    newList: [],
    hotList: [],
    user:{}
  },
    // 页面切换
    changeItem: function(e) {
      this.setData({
        item: e.target.dataset.item
      })
    },
    // tab切换
    changeTab: function(e) {   
      this.setData({
        tab: e.detail.current
      })
    },
    onLoad(){
      wx.request({
        url: 'http://localhost:9000/article',
        method: 'GET',
        success: (result) => {
          this.setData({           
            healthlist: result.data
          })        
        },
      })
      wx.request({
        url: 'http://localhost:9000/article/hotList',
        method: 'GET',
        success: (result) => {
          this.setData({           
            hotList: result.data
          })        
        },
      })
      wx.request({
        url: 'http://localhost:9000/user/1',
        method: "GET",
        success: (result) => {
          this.setData({           
            user: result.data
          })        
        },
      })
     
  },

  search:function(){
    wx.navigateTo({
      url: '/pages/article/sousuo/suosuo',
    })
  },
    detail:function(e){
      //console.log(e);
      wx.setStorage({
        key: "detail",
        data: this.data.healthlist[e.currentTarget.id-1]
      }),
      wx.setStorage({
        key: "user",
        data: this.data.user
      }),
      wx.navigateTo({
        url: '/pages/article/zhishi/zhishi',
      })
    },

    hot_detail:function(e){
      wx.setStorage({
        key: "detail",
        data: this.data.hotList[e.currentTarget.id-1]
      }),
      wx.navigateTo({
        url: '/pages/article/zhishi/zhishi',
      })
    },

    giveAdd:function(e){

      //console.log(this.data);
      var data=this.data.healthlist[e.target.id-1]
      
      if(data.is_add){
        data.add--
        data.is_add=false
      }else{       
        data.add++
        data.is_add=true
      }

      wx.request({
        url: 'http://localhost:9000/article',
        method:'POST',
        data: data,
        success:()=>{
          //console.log(res);
          this.onLoad()
        }
      })
    },

    //返回时重新加载数据
    onShow() {
      wx.request({
        url: 'http://localhost:9000/article',
        method: 'GET',
        success: (result) => {
          this.setData({           
            healthlist: result.data
          })        
        },
      })
    },






})
